<?php

namespace App\Service;

use App\Constants\ErrorCode;
use App\Model\Channel;
use App\Model\LinkChannel;
use App\Model\LinkFile;
use App\Model\LinkIndustry;
use App\Model\Product;
use App\Model\ProductSpecKey;
use App\Model\ProductSpecValue;
use Hyperf\DbConnection\Db;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

class LinkChannelService extends BaseService
{
    //   新增单条  包括关联关系
    /**
     * @param array $channel_insert
     * @param int $product_id
     * @param int $type
     * @return bool
     */
    public function insert_or_update(array  $channel_insert,$product_id,$type=1):bool
    {
        $instance = $this->getModelInstance();
        foreach ($channel_insert as $key=>$value){
            if(is_array($value)){
//                数组的说明是修改
                $this->update_insert([
                    'link_id'=>$product_id,
                    'channel_id'=>$value['channel_id'],
                    'type'=>$type,
                ],[
                    'link_id'=>$product_id,
                    'channel_id'=>$value['channel_id'],
                    'type'=>$type,
                ]);
            }else{
                //                非数组的说明是id
                $this->update_insert([
                    'link_id'=>$product_id,
                    'channel_id'=>$value,
                    'type'=>$type,
                ],[
                    'link_id'=>$product_id,
                    'channel_id'=>$value,
                    'type'=>$type,
                ]);
            }


        }
        return true;
    }
    /**
     * @param int $product_id
     * @return array
     */
    public function getLink($product_id,$type=1)
    {
       $arr = [];
       $where = [];
       $where['product_id'] = $product_id;
       $where['type'] = $type;
       $arr = $this->getWhere($where);
       return $arr;
    }
}